package com.atguigu.yygh.hosp.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.Exception.YyghException;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.result.Result;
import com.atguigu.yygh.utlis.HttpRequestHelper;
import com.atguigu.yygh.utlis.MD5;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@Api(tags = "医院管理Api接口")
@RequestMapping("/api/hosp")
//@CrossOrigin//不需要跨域注解 原因: 这是一个接口 hospital-manage模块通过httpclient 发送请求
// httpclient因为没有通过浏览器,所以不会存在跨域问题
public class ApiController {

    @Autowired
    HospitalService hospitalService;

    @Autowired
    HospitalSetService hospitalSetService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    ScheduleService scheduleService;
    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request){//request:请求对象,
        //获取医院模拟系统传递的map集合
        //获取所有提交过来的参数,参数名为key,值为value
        Map<String, String[]> parameterMap = request.getParameterMap();
        // Map<String, String[]> 变成 Map<String, Object> 使用HttpRequestHelper工具类
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //在传递过程中LogoData中的+被转换成"",因此我们需要转回来
        String logoData = (String) paramMap.get("logoData");//先获取
        String s = logoData.replaceAll(" ", "+");//再替换掉其中的" "
        paramMap.put("logoData",s);//在添加到Map集合中

        //从parameterMap集合中获取所有数据,添加到MongoDB中
        //将paramMap集合转换成Json字符串
        String jsonString = JSONObject.toJSONString(paramMap);

        //使用Json工具(fastJson)将Json字符串转成实体类对象(Hospital)
        Hospital hospital = JSONObject.parseObject(jsonString, Hospital.class);

        //获取传递过来的signKey
        String signKey = (String) paramMap.get("sign");

        //将两个singKey进行比较,
        boolean b = this.compareKey(signKey, hospital.getHoscode());

        if (b){//sign_key一致时:
            //调用方法添加到MongoDb中
            hospitalService.save(hospital);
        }else {//不一致时,抛出异常
            throw new YyghException(20001,"sign_key码错误");
        }
        return Result.ok();
    }

    //查询医院接口
    @PostMapping("hospital/show")
    public Result showHosp(HttpServletRequest request){
        //获取医院模拟系统传递的map集合
        //获取所有提交过来的参数,参数名为key,值为value
        Map<String, String[]> parameterMap = request.getParameterMap();
        // Map<String, String[]> 变成 Map<String, Object> 使用HttpRequestHelper工具类
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //获取传递过来的校验码
        String sign = (String) paramMap.get("sign");
        //签名校验
        boolean b = this.compareKey(sign, hoscode);
        if (!b){//如果不一致时,返回错误信息
            return Result.fail("signKey校验码错误!");
        }
        //判断hoscode是否为空
        if (StringUtils.isEmpty(hoscode)){//如果为空,抛出异常
            throw new YyghException(20001,"数据参数错误");
        }else {//如果不为空,在执行查询操作
             Hospital hospital =  hospitalService.getHospByHoscode(hoscode);
            return Result.ok(hospital);
        }

    }

    //上传科室
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //获取signKey
        String sign = (String) paramMap.get("sign");
        //进行效验
        boolean b = this.compareKey(sign, hoscode);
        if (!b){//如果不一致
            return Result.fail("signKey校验码错误!");
        }

        //将paramMap转换成json字符串
        String jsonString = JSONObject.toJSONString(paramMap);

        //将json字符串转换成Department 对象
        Department department = JSONObject.parseObject(jsonString, Department.class);

        departmentService.save(department);

        return Result.ok();
    }

    //查询科室
    @PostMapping("department/list")
    public Result departmentList(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //获取 传递过来的sign校验码
        String sign = (String) paramMap.get("sign");
        //获取hoscode医院编码
        String hoscode = (String) paramMap.get("hoscode");
        //进行校验
        boolean b = this.compareKey(sign, hoscode);
        if (!b){//如果不一致
            return Result.fail("校验码错误!");
        }
        //当前页
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        //每页记录数
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));
        String depcode = (String) paramMap.get("depcode");

        //创建departmentQueryVo 对象, 用来存放 查询条件
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);//设置hoscode 医院编码
        departmentQueryVo.setDepcode(depcode);//设置depcode 科室编码
        //进行分页查询
        Page<Department> departmentPage = departmentService.selectPage(page,limit,departmentQueryVo);
        return Result.ok(departmentPage);
    }
    @PostMapping("department/remove")
    private Result removeDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //获取 传递过来的sign校验码
        String sign = (String) paramMap.get("sign");
        //获取hoscode医院编码
        String hoscode = (String) paramMap.get("hoscode");
        //进行效验
        boolean b = this.compareKey(sign, hoscode);
        if (!b){//如果不一致时
            return Result.fail("校验码错误");
        }
        String depcode = (String) paramMap.get("depcode");//获取科室编码
        departmentService.removeByHoscodeAndDepcode(hoscode,depcode);


        return Result.ok();
    }
    //上传排班
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //获取 传递过来的sign校验码
        String sign = (String) paramMap.get("sign");
        //获取hoscode医院编码
        String hoscode = (String) paramMap.get("hoscode");
        //进行效验
        boolean b = this.compareKey(sign, hoscode);
        if (!b){//如果不一致时
            return Result.fail("校验码错误");
        }
        //将paramMap集合转换成json字符串
        String jsonString = JSONObject.toJSONString(paramMap);
        //将json字符串转转换成Schedule对象
        Schedule schedule = JSONObject.parseObject(jsonString, Schedule.class);
        scheduleService.save(schedule);

        return Result.ok();
    }
    @PostMapping("schedule/list")
    public Result scheduleList(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //获取 传递过来的sign校验码
        String sign = (String) paramMap.get("sign");
        //获取hoscode医院编码
        String hoscode = (String) paramMap.get("hoscode");
        //进行校验
        boolean b = this.compareKey(sign, hoscode);
        if (!b){//如果不一致
            return Result.fail("校验码错误!");
        }
        //当前页
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        //每页记录数
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));
        String depcode = (String) paramMap.get("depcode");

        //创建departmentQueryVo 对象, 用来存放 查询条件
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);//设置hoscode 医院编码
        scheduleQueryVo.setDepcode(depcode);//设置depcode 科室编码


        //进行分页查询
        Page<Schedule> schedulePage = scheduleService.selectPage(page,limit,scheduleQueryVo);
        return Result.ok(schedulePage);

    }
    @PostMapping("schedule/remove")
    public Result scheduleRemove(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //获取 传递过来的sign校验码
        String sign = (String) paramMap.get("sign");
        //获取hoscode医院编码
        String hoscode = (String) paramMap.get("hoscode");
        //进行校验
        boolean b = this.compareKey(sign, hoscode);
        if (!b){//如果不一致
            return Result.fail("校验码错误!");
        }
        String hoscode1 = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        scheduleService.scheduleRemove(hoscode,hosScheduleId);
        return Result.ok();
    }

    /**
     * 功能: 对signKey进行比较
     * 参数一:医院模拟系统传过来的signKey
     * 参数二:医院模拟系统传过来的hoscode
     * */
    private boolean compareKey(String signKey,String hoscode){
        String getSignKey = hospitalSetService.getSignkey(hoscode);//获取本地数据的singKey
        String md5Sign = MD5.encrypt(getSignKey);//进行md5加密
        // 加密原因: 医院模拟系统传递过来的signKey也进行过加密

        if (signKey.equals(md5Sign)){//将从医院模拟系统传过来的signKey和从本地数据库查到的进行比较
            //如果一致返回true
            return true;
        }
        //否则返回fasle;
        return false;
    }



}
